﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace QLThuChi.OtherForms
{
    #region Using
    using QLThuChi.Models;
    using QLThuChi.Repositories;
    using QLThuChi.Helpers;
    using QLThuChi.OtherForms;
    using DevComponents.DotNetBar;
    using QLThuChi.Reports;
    #endregion
    public partial class FrmThongKePhieuChiLuong : Office2007Form
    {
        // Hidden files are used to store ids 
        private DevComponents.DotNetBar.Controls.TextBoxX txtMaCongTrinh;

        public FrmThongKePhieuChiLuong()
        {
            InitializeComponent();
            InitHiddenFields();
        }

        /// <summary>
        /// Init hidden fields
        /// </summary>
        public void InitHiddenFields()
        {
            // Add a new textbox
            txtMaCongTrinh = new DevComponents.DotNetBar.Controls.TextBoxX
            {
                Name = "txtMaCongTrinh",
                Text = ""
            };
            txtMaCongTrinh.Visible = false;

        }

        private void FrmThongKePhieuChiLuong_Load(object sender, EventArgs e)
        {
        }

        private void btnChonCongTrinh_Click(object sender, EventArgs e)
        {
            FrmQuanLyCongTrinh frm = new FrmQuanLyCongTrinh(true);
            frm.Handler += GetCongTrinh;
            frm.ShowDialog();
        }

        public void GetCongTrinh(object sender, EventArgs e)
        {
            var eventType = (MyEvent)e;
            string[] comp = eventType.Data.Split(new char[] { '#' });

            txtMaCongTrinh.Text = comp[0];
            txtCongTrinh.Text = comp[1];
            LoadGiaDoanBelongToCongTrinh();
        }

        public void LoadGiaDoanBelongToCongTrinh()
        {
            var lstItem = CongTrinh_GiaiDoanRepository.SelectByMaCongTrinh(int.Parse(txtMaCongTrinh.Text));
            cbxGiaiDoan.Items.Clear();

            var all = new CongTrinh_GiaiDoan
            {
                MaGiaiDoan = 0
            };
            all.GiaiDoan = new GiaiDoan
            {
                TenGiaiDoan = "---Tất cả---"
            };
            cbxGiaiDoan.Items.Add(all);

            foreach (var item in lstItem)
            {
                cbxGiaiDoan.Items.Add(item);
            }

            cbxGiaiDoan.SelectedIndex = 0;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnBaoBieu_Click(object sender, EventArgs e)
        {
            if (cbxGiaiDoan.SelectedIndex != -1)
            {
                var giaidoan = (CongTrinh_GiaiDoan)cbxGiaiDoan.SelectedItem;
                var lstItem = new List<PhieuChi_LuongNV>();
                // Select all of giai doan
                if (giaidoan.MaCongTrinhGiaiDoan == 0)
                {
                    lstItem = PhieuChi_LuongNVRepository.SelectByMaCongTrinh(int.Parse(txtMaCongTrinh.Text));
                }
                else
                {
                    lstItem = PhieuChi_LuongNVRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                }

                LapBaoBieu(lstItem);
            }
        }

        public void LapBaoBieu(List<PhieuChi_LuongNV> lstItem)
        {
            var dt = new DataTable();
            dt.Columns.Add("STT");
            dt.Columns.Add("GiaiDoan");
            dt.Columns.Add("NhanCong");
            dt.Columns.Add("SoNgayCong");
            dt.Columns.Add("Luong");
            dt.Columns.Add("LuongNhanDuoc");
            dt.Columns.Add("NguoiLapPhieu");
            dt.Columns.Add("NgayLapPhieu");

            for (int i = 0; i < lstItem.Count; i++)
            {
                dt.Rows.Add((i + 1).ToString(), lstItem[i].GiaiDoan_NhanCong.CongTrinh_GiaiDoan.GiaiDoan.TenGiaiDoan, lstItem[i].GiaiDoan_NhanCong.NhanCong.TenNhanCong, lstItem[i].SoNgayCong.ToString(), String.Format("{0:#,##0;Nothing}", lstItem[i].Luong), String.Format("{0:#,##0;Nothing}", lstItem[i].Luong * lstItem[i].SoNgayCong),  lstItem[i].NguoiDung.TenNguoiDung, String.Format("{0:dd/MM/yyyy}", lstItem[i].NgayLapPhieu.Value));
            }

            DataView objTimeSheetDV = new System.Data.DataView(dt);
            objTimeSheetDV.AllowNew = false;
            dtgvDataList.AutoGenerateColumns = false;
            dtgvDataList.DataSource = objTimeSheetDV;

            dtgvDataList.ClearSelection();
        }

        private void btnInBieu_Click(object sender, EventArgs e)
        {
            if (cbxGiaiDoan.SelectedIndex != -1)
            {
                var giaidoan = (CongTrinh_GiaiDoan)cbxGiaiDoan.SelectedItem;
                var lstItem = new List<PhieuChi_LuongNV>();
                // Select all of giai doan
                if (giaidoan.MaCongTrinhGiaiDoan == 0)
                {
                    lstItem = PhieuChi_LuongNVRepository.SelectByMaCongTrinh(int.Parse(txtMaCongTrinh.Text));
                }
                else
                {
                    lstItem = PhieuChi_LuongNVRepository.SelectByMaCongTrinhGiaiDoan(giaidoan.MaCongTrinhGiaiDoan);
                }

                FrmPrintPhieuChiLuong frm = new FrmPrintPhieuChiLuong(lstItem, txtCongTrinh.Text);
                frm.ShowDialog();
            }
        }
    }
}
